// miniprogram/pages/index/company/start_job/start_job.js
const db=wx.cloud.database()
const _=db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company:{},
    btn: true
  },
  check: function(event){
    const that = this;
    let tem="";
    wx.requestSubscribeMessage({
      tmplIds: ['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'], 
      success: res=>{
        tem = res['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'];
        if(tem=="accept"){
            wx.showToast({
            title: '已开启',
          })
          that.startJob(event);
        }else{
          wx.showToast({
            title: '已拒绝',
          })
        }
      },
      fail: res=>{
          wx.showToast({
            title: '已拒绝',
          })
      }
    })
  },
  startJob: function(event){
    this.setData({btn: true})
    
    const company_obj=this.data.company.company_obj;
    const job_name=event.detail.value.job_name;
    const job_location=event.detail.value.job_location;
    const job_pay=event.detail.value.job_pay;
    const job_num=event.detail.value.job_num;
    const job_main=event.detail.value.job_main;
    const openid=company_obj.openid;
    const cpny_name=company_obj.cpny_name;
    const cpny_location=company_obj.cpny_location;
    const cpny_man=company_obj.cpny_man;
    const cpny_phone=company_obj.cpny_phone;
    const cpny_mail=company_obj.cpny_mail;
    const cpny_profile=company_obj.cpny_profile;
    const cpny_type=company_obj.cpny_type;
    const images=company_obj.images;

    const today = new Date();
    const year = today.getFullYear();
    const mouth = today.getMonth() + 1;
    const day = today.getDate();
    let mouth_str='';
    let day_str='';
    if(mouth<10){
     mouth_str="0"+mouth;
    }else{ 
      mouth_str=mouth;
    }
    if(day<10){
      day_str="0"+day;
     }else{ 
       day_str=day;
     }
     const date_str=year+"-"+mouth_str+"-"+day_str;
    const job_obj={
      job_name: job_name,
      job_location: job_location,
      job_pay: job_pay,
      job_num: job_num,
      job_main: job_main,
      openid: openid,
      cpny_name: cpny_name,
      cpny_location: cpny_location,
      cpny_man: cpny_man,
      cpny_phone: cpny_phone,
      cpny_mail: cpny_mail,
      cpny_profile: cpny_profile,
      cpny_type: cpny_type,
      images: images,
      job_type: -1,
      job_note: '',
      job_start_date: date_str
    }
    db.collection("job").add({
      data: {
       
         job_obj: job_obj
      }
     
    }).then(res=>{
     
      wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: 'oAuf35SpOxe24BaPIPUZ4_2wDRXU',
          title: "岗位发布",
          content: cpny_name,
          note:  job_obj.job_name
        }
      })
      getApp().tosSuccess("发布成功!")
     
      setTimeout(function () {
        wx.navigateBack()
       }, 1000)
    })
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      company: JSON.parse(decodeURIComponent(options.obj))
     })
    
     
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({btn: false})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})